package com.njit.mailm.core.controller;

import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.core.bean.MessageWrap;
import com.njit.mailm.core.bean.User;
import com.njit.mailm.core.service.ClassifyService;
import org.aspectj.weaver.patterns.HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/5/10.
 */
@RequestMapping("/classify")
@Controller
public class ClassifyController {

    @Autowired
    private ClassifyService classifyService;

    @RequestMapping(value = "showView",method = RequestMethod.GET)
    public String showView(){
        return "classifyMailList";
    }

    @ResponseBody
    @RequestMapping(value = "showAllUnread",method = RequestMethod.GET)
    public List<MessageWrap> showAllUnreadMessages(HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return classifyService.getAllUnreadMessage(user.getId());
    }
    @RequestMapping(value = "messageDetails",method = RequestMethod.POST)
    public String messageDetails(String id, Model model){
        MessageWrap messageWrap = classifyService.getMessageById(id);
        model.addAttribute("messageDetail",messageWrap);
        return "classifyMailRead";

    }

    @ResponseBody
    @RequestMapping(value = "showAllStarList",method = RequestMethod.GET)
    public List<MessageWrap> showAllStarMessage(HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return classifyService.getAllStarMessage(user.getId());
    }

    @ResponseBody
    @RequestMapping(value = "showAllMessage",method = RequestMethod.GET)
    public List<MessageWrap> showAllMessage(HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return classifyService.showAllMessage(user.getId());
    }

    @ResponseBody
    @RequestMapping(value = "addMessage" ,method = RequestMethod.POST)
    public boolean addMessage(String name,String messageId, HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);

        return classifyService.addMessage(name,messageId,user);
    }

    @ResponseBody
    @RequestMapping(value = "showMessageInClassify",method = RequestMethod.POST)
    public List<MessageWrap> showMessageInClassify(String classify,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return classifyService.showMessageInClassify(classify,user);
    }
}
